Initializing an Application
When your application first starts up, and even before you begin to receive and process events describing the user's actions, you need to do some initial setting up. As you've already seen (page 3), you need to initialize some of the Macintosh Toolbox managers. You also need to set up your menu bar and menus, and perform some other standard initialization. Listing 4-1 shows the code executed by the Venn Diagrammer application when it first starts up.Listing 4-1 Initializing your application
DoInitManagers; {initialize Toolbox managers} DoSetupMenus; {initialize menus} gDone := FALSE; {initialize global variables} gNumDocWindows := 0; {initialize count of open doc windows} gPrefsDialog := NIL; {initialize ptr to Preferences dialog} gAppsResourceFile := CurResFile; {get refnum of the app's resource file} gPreferencesFile := -1; {initialize res ID of preferences file} DoReadPrefs; {read the user's preference settings} DoVennInit; DoMainEventLoop; {and then loop forever...}The first thing the Venn Diagrammer application does is call the application-defined routineDoInitManagers
to set up its application partition and initialize several Toolbox managers. Then it callsDoSetupMenus
to create its menu bar and menus. (See Listing 8-1 on page 155 for the definition ofDoSetupMenus
.)After its menu bar has been created, Venn Diagrammer initializes several global variables and reads the user's current preferences from a preferences file. Then the application calls another routine,
DoVennInit
, to handle any other initialization. This includes defining the rectangles and regions in a Venn diagram window and displaying a window.
Once the application has initialized itself, it starts executing its main event loop by calling the
- Note
- The
DoVennInit
procedure is not defined in this book.![]()
DoMainEventLoop
procedure. In the main event loop, the application calls the Event Manager to get an event, responds to the event, then loops back to repeat the process. See Listing 4-4 on page 77 for a sample event loop.Listing 4-2 defines the
DoInitManagers
routine. It begins by calling two Memory Manager routines to expand the heap zone to its limit and to create an additional block of master pointers.Listing 4-2 Initializing the main Toolbox Managers
PROCEDURE DoInitManagers; BEGIN MaxApplZone; {extend heap zone to limit} MoreMasters; {get 64 more master pointers} InitGraf(@thePort); {initialize QuickDraw} InitFonts; {initialize Font Manager} InitWindows; {initialize Window Manager} InitMenus; {initialize Menu Manager} TEInit; {initialize TextEdit} InitDialogs(NIL); {initialize Dialog Manager} FlushEvents(everyEvent, 0); {clear event queue} InitCursor; {initialize cursor to arrow} END;ThenDoInitManagers
calls the standard Toolbox initialization routines. Finally, it clears the event queue and calls the QuickDraw routineInitCursor
to make sure that the cursor is the standard arrow cursor.